package com.smssalama.gui;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.TextField;

import com.smssalama.AlertManager;
import com.smssalama.SMSSalama;
import com.smssalama.SMSSalamaSession;
import com.smssalama.Utils;
import com.smssalama.i18n.CommandLabels;
import com.smssalama.i18n.ErrorMessages;
import com.smssalama.i18n.Labels;
import com.smssalama.i18n.ScreenTitles;
import com.smssalama.storage.contact.Contact;

/**
 * A form for editing a contact.
 * 
 * @author Arnold P. Minde
 *
 */
public class ContactEditor extends Form implements CommandListener {
	private Labels LABELS = Labels.labels();
	private CommandLabels CMD_LABELS = CommandLabels.cmdLabels();
	
	Displayable back;
	Contact contact = null;

	TextField phoneNumberField = new TextField(LABELS.phoneNumber(), "",
			15, TextField.ANY);
	TextField nameField = new TextField(LABELS.contactName(), "", 15,
			TextField.ANY);
	TextField passwordField = new TextField(LABELS.password(), "", 15,
			TextField.ANY);
	TextField emailField = new TextField(LABELS.email(), "", 15,
			TextField.ANY);

	Command backCmd = new Command(CMD_LABELS.back(), Command.BACK,
			1);
	Command exitCmd = new Command(CMD_LABELS.exit(), Command.EXIT,
			1);
	Command saveCmd = new Command(CMD_LABELS.save(), Command.OK, 1);

	public ContactEditor(Displayable back) {
		super(ScreenTitles.screenTitles().contactEditorNew());
		this.back = back;
		init();
	}

	public ContactEditor(Displayable back, Contact contact) {
		super(ScreenTitles.screenTitles().contactEditor());
		this.back = back;
		this.contact = contact;
		init();
		this.phoneNumberField.setString(contact.getPhoneNumber());
		this.nameField.setString(contact.getContactName());
		this.passwordField.setString(contact.getSharedKey());
		this.emailField.setString(contact.getEmail());
		// contact.setNote("");
	}

	public void init() {
		setCommandListener(this);

		this.phoneNumberField.setConstraints(TextField.PHONENUMBER);
		this.phoneNumberField.setMaxSize(30);
		this.phoneNumberField.setString("");
		this.nameField.setMaxSize(30);
		this.passwordField.setMaxSize(50);
		this.emailField.setConstraints(TextField.EMAILADDR);
		this.emailField.setMaxSize(100);

		this.append(this.nameField);
		this.append(this.phoneNumberField);
		this.append(this.passwordField);
		this.append(this.emailField);

		this.addCommand(this.backCmd);
		this.addCommand(this.saveCmd);
		this.addCommand(this.exitCmd);
	}

	public void commandAction(Command command, Displayable displayable) {
		if (command == this.backCmd) {
			AlertManager.showDisplayable(this.back);
		} else if (command == this.exitCmd) {
			SMSSalama.quitApp();
		} else if (command == this.saveCmd) {
			try {
				save();
				AlertManager.showDisplayable(this.back);
			} catch (Exception e) {
				e.printStackTrace();
				AlertManager.showError(this, e);
			}
		}
	}

	protected void validate() throws Exception {
		if (this.nameField.getString().trim().length() == 0) {
			throw new Exception(ErrorMessages.errors.invalidName());
		}
		if (!Utils.isValidaPhoneNumber(Utils
				.fixPhoneNumberPrefix(this.phoneNumberField.getString()))) {
			throw new Exception(ErrorMessages.errors
					.invalidPhoneNumber(this.phoneNumberField.getString()));
		}
	}
	public Contact getContact(){
		return this.contact;
	}
	public void save() throws Exception {
		validate();
		if (this.contact == null) {
			this.contact = new Contact();
		}
		this.contact.setPhoneNumber(this.phoneNumberField.getString());
		this.contact.setContactName(this.nameField.getString());
		this.contact.setSharedKey(this.passwordField.getString());
		this.contact.setEmail(this.emailField.getString());
		this.contact.setNote("");
		SMSSalamaSession.getSession().getUserSettings().getContactsStore()
				.persistStorable(this.contact);
	}
}
